package br.ucb.ucblive.HIB;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.ucb.ucblive.interfaces.HIBInterface;

public class BaseHIB implements HIBInterface {

	@Override
	public void salvar(Object obj) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.saveOrUpdate(obj);
		transaction.commit();
		session.close();
	}

	@Override
	public void excluir(Object obj) {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		session.delete(obj);
		transaction.commit();
		session.close();
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> listar() {
		Session session = HibernateUtil.getSession();
		try {
			return session.createCriteria(Object.class).list();
		} finally {
			session.close();
		}
	}

	@Override
	public Object consultar(int id) {
		Session session = HibernateUtil.getSession();
		try {
			Object obj = (Object) session.get(Object.class,id);
			return obj;
		} finally {
			session.close();
		}
	}

}
